package com.hangzhou.xyj.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.event.ServerRespEvent;
import com.hangzhou.xyj.net.handler.AddFollowHandler;
import com.hangzhou.xyj.net.handler.CancelFollowHandler;
import com.hangzhou.xyj.net.handler.CommentHandler;
import com.hangzhou.xyj.net.handler.GetBookListHandler;
import com.hangzhou.xyj.net.handler.GetCommunitySingDynamicListHandler;
import com.hangzhou.xyj.net.handler.GetFansUsersHandler;
import com.hangzhou.xyj.net.handler.GetFollowUsersHandler;
import com.hangzhou.xyj.net.handler.GetGroupMainHandler;
import com.hangzhou.xyj.net.handler.GetListHandler;
import com.hangzhou.xyj.net.handler.GetLocationHobbyHandler;
import com.hangzhou.xyj.net.handler.GetUserByCatHandler;
import com.hangzhou.xyj.net.handler.ListenHandler;
import com.hangzhou.xyj.net.handler.LoginHandler;
import com.hangzhou.xyj.net.handler.MyOrderedSingHandler;
import com.hangzhou.xyj.net.handler.PartInGroupHandler;
import com.hangzhou.xyj.net.handler.QueryGroupListHandler;
import com.hangzhou.xyj.net.handler.RegistHandler;
import com.hangzhou.xyj.net.handler.ReportHandler;
import com.hangzhou.xyj.net.handler.ResetPasswordHandler;
import com.hangzhou.xyj.net.handler.SmsShortCodeHandler;
import com.hangzhou.xyj.net.handler.TransferHandler;
import com.hangzhou.xyj.net.handler.UpdateHobbyHandler;
import com.hangzhou.xyj.net.handler.UpdateNameHandler;
import com.hangzhou.xyj.net.handler.UpdatePasswordHandler;
import com.hangzhou.xyj.net.handler.UpdateSexHandler;
import com.hangzhou.xyj.net.handler.UpdateSignatureHandler;
import com.hangzhou.xyj.net.handler.UpdateTeacherHandler;
import com.hangzhou.xyj.net.handler.UpdateZoneHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.listener.HttpResponseListener;
import com.hangzhou.xyj.net.request.AddFollowReq;
import com.hangzhou.xyj.net.request.CancelFollowReq;
import com.hangzhou.xyj.net.request.CommentReq;
import com.hangzhou.xyj.net.request.GetBookListReq;
import com.hangzhou.xyj.net.request.GetCommunitySingDynamicListReq;
import com.hangzhou.xyj.net.request.GetFansUsersReq;
import com.hangzhou.xyj.net.request.GetFollowUsersReq;
import com.hangzhou.xyj.net.request.GetGroupMainReq;
import com.hangzhou.xyj.net.request.GetListReq;
import com.hangzhou.xyj.net.request.GetLocationHobbyReq;
import com.hangzhou.xyj.net.request.GetUserByCatReq;
import com.hangzhou.xyj.net.request.ListenReq;
import com.hangzhou.xyj.net.request.LoginReq;
import com.hangzhou.xyj.net.request.MyOrderedSingReq;
import com.hangzhou.xyj.net.request.PartInGroupReq;
import com.hangzhou.xyj.net.request.QueryGroupListReq;
import com.hangzhou.xyj.net.request.RegisterReq;
import com.hangzhou.xyj.net.request.ReportReq;
import com.hangzhou.xyj.net.request.ResetPasswordReq;
import com.hangzhou.xyj.net.request.SmsShortCodeReq;
import com.hangzhou.xyj.net.request.TransferReq;
import com.hangzhou.xyj.net.request.UpdateHobbyReq;
import com.hangzhou.xyj.net.request.UpdateNameReq;
import com.hangzhou.xyj.net.request.UpdatePasswordReq;
import com.hangzhou.xyj.net.request.UpdateSexReq;
import com.hangzhou.xyj.net.request.UpdateSignatureReq;
import com.hangzhou.xyj.net.request.UpdateTeacherReq;
import com.hangzhou.xyj.net.request.UpdateZoneReq;
import com.hangzhou.xyj.net.request.base.BaseRequest;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.utils.ToastUtil;

import org.greenrobot.eventbus.Subscribe;

/**
 * @author : neo.duan
 * @date : 	 2016/2/17
 * @desc : 关于服务器API的测试页面
 */
public class APITestActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvRequest;
    public HttpResponseListener responseListener = new MyHttpResponseListener() {
        @Override
        public void onResponse(int code, Object jsonObject, int tag) {
            switch (code) {
                case RESPONSE_START:
                    showLoadingDialog();
                    if (jsonObject != null && jsonObject instanceof BaseRequest) {
                        BaseRequest request = (BaseRequest) jsonObject;
                        mTvRequest.setText(request.getUrl());
                    }
                    break;
                case RESPONSE_SUCCESS:
//                    hideLoadingDialog();
                    if (jsonObject != null) {
//                        ToastUtil.show(jsonObject.toString());
                    } else {
//                        ToastUtil.show("model为空");
                    }
                    break;
                case RESPONSE_FAIL:
                    ToastUtil.show(jsonObject.toString());
                    break;
                default:
                    hideLoadingDialog();
                    break;
            }
        }
    };
    private TextView mTvResult;

    @Override
    public void initTop() {
        enableTitle(true, "测试API");
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_api_test);
    }

    @Override
    public void initViews() {
        mTvRequest = findView(R.id.tv_request_url);
        mTvResult = findView(R.id.tv_result);
    }

    @Override
    public void initDatas() {

    }

    @Override
    public void initListeners() {
        findViewById(R.id.btn_update_sign).setOnClickListener(this);
        findView(R.id.btn_test_login).setOnClickListener(this);
        findView(R.id.btn_test_register).setOnClickListener(this);
        findView(R.id.btn_test_sms_code).setOnClickListener(this);
        findViewById(R.id.btn_test_get_info_list).setOnClickListener(this);
        findViewById(R.id.btn_get_users_list).setOnClickListener(this);
        findViewById(R.id.btn_reset_password).setOnClickListener(this);
        findViewById(R.id.btn_update_password).setOnClickListener(this);
//        findViewById(R.id.btn_update_head).setOnClickListener(this);
        findViewById(R.id.btn_register_third_account).setOnClickListener(this);
        findViewById(R.id.btn_get_fans).setOnClickListener(this);
        findViewById(R.id.btn_add_follow).setOnClickListener(this);
        findViewById(R.id.btn_cancel_follow).setOnClickListener(this);
        findViewById(R.id.btn_test_report).setOnClickListener(this);
        findViewById(R.id.btn_after_share).setOnClickListener(this);
        findViewById(R.id.btn_test_collect).setOnClickListener(this);
        findViewById(R.id.btn_my_like).setOnClickListener(this);
        findViewById(R.id.btn_test_stop_listen).setOnClickListener(this);
        findViewById(R.id.btn_test_comment).setOnClickListener(this);
        findViewById(R.id.btn_test_query).setOnClickListener(this);
        findViewById(R.id.btn_test_drama_category).setOnClickListener(this);
        findViewById(R.id.btn_test_find_group_main).setOnClickListener(this);
        findViewById(R.id.btn_test_group_detail).setOnClickListener(this);
        findViewById(R.id.btn_test_query_group).setOnClickListener(this);
        findViewById(R.id.btn_test_part_group).setOnClickListener(this);
        findViewById(R.id.btn_test_book_list).setOnClickListener(this);
        findViewById(R.id.btn_test_update_sex).setOnClickListener(this);
        findViewById(R.id.btn_test_update_name).setOnClickListener(this);
        findViewById(R.id.btn_test_update_hobby).setOnClickListener(this);
        findViewById(R.id.btn_test_update_teacher).setOnClickListener(this);
        findViewById(R.id.btn_test_update_zone).setOnClickListener(this);
        findViewById(R.id.btn_test_get_community_dynamic).setOnClickListener(this);
        findViewById(R.id.btn_test_location_hobby).setOnClickListener(this);
        findViewById(R.id.btn_test_publish).setOnClickListener(this);
        findViewById(R.id.btn_test_ordered_sing).setOnClickListener(this);
    }

    /**
     * 服务器数据返回事件
     *
     * @param event
     */
    @Subscribe
    public void onEventMainThread(ServerRespEvent event) {
        Log.d(TAG, "server response----> " + event.getData().toString());
        mTvResult.setText(event.getData().toString());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_test_login: //登录
                HttpLoader.getInstance().sendRequest(new LoginHandler(new LoginReq("18942336125", "123456"), responseListener, 0));
                break;
            case R.id.btn_test_register: //注册
                RegisterReq req = new RegisterReq("13336167959", "123456", "706581");
                HttpLoader.getInstance().sendRequest(new RegistHandler(req, responseListener, 0));
                break;
            case R.id.btn_test_sms_code: //获取验证码：这里需要服务器优化下：已经注册过的手机号应该不需要发送验证码了
                SmsShortCodeReq smsReq = new SmsShortCodeReq("13336167959", SmsShortCodeReq.TYPE_REGIST);
                HttpLoader.getInstance().sendRequest(new SmsShortCodeHandler(smsReq, responseListener, 0));
                break;
//            case R.id.btn_register_third_account: //注册第三方账户
//                RegistThirdAccountReq registThirdAccountReq = new RegistThirdAccountReq("MayDay", RegistThirdAccountReq.TYPE_WEIXIN);
//                HttpLoader.getInstance().sendRequest(new RegistThirdAccountHandler(registThirdAccountReq, responseListener, 0));
//                break;
            case R.id.btn_test_get_info_list://活动，演出，资讯列表
//                GetListOfInfoReq getListOfInfoReq = new GetListOfInfoReq(GetListOfInfoReq.TYPE_HOT_SPOT);
//                HttpLoader.getInstance().sendRequest(new GetListOfInfoHandler(getListOfInfoReq, responseListener, 0));
                break;
            case R.id.btn_get_users_list://获取关注人列表
                GetFollowUsersReq getFollowUsersReq = new GetFollowUsersReq(17, 1, 1);
                HttpLoader.getInstance().sendRequest(new GetFollowUsersHandler(getFollowUsersReq, responseListener, 0));
                break;
            case R.id.btn_reset_password://重置密码
                ResetPasswordReq resetPasswordReq = new ResetPasswordReq("13336167959", "654321", "123456");
                HttpLoader.getInstance().sendRequest(new ResetPasswordHandler(resetPasswordReq, responseListener, 0));
                break;
            case R.id.btn_update_password://更新密码
                UpdatePasswordReq updatePasswordReq = new UpdatePasswordReq("17", "123456", "654321");
                HttpLoader.getInstance().sendRequest(new UpdatePasswordHandler(updatePasswordReq, responseListener, 0));
                break;
            case R.id.btn_update_head://更新头像 TODO 暂时不处理
                break;
            case R.id.btn_update_sign://更新签名
                UpdateSignatureReq updateSignatureReq = new UpdateSignatureReq(17, "你不是真正的快乐");
                HttpLoader.getInstance().sendRequest(new UpdateSignatureHandler(updateSignatureReq, responseListener, 0));
                break;
            case R.id.btn_get_fans://获取粉丝列表
                GetFansUsersReq getFansUsersReq = new GetFansUsersReq(17, 1, 1);
                HttpLoader.getInstance().sendRequest(new GetFansUsersHandler(getFansUsersReq, responseListener, 0));
                break;
            case R.id.btn_add_follow://添加关注
                AddFollowReq addFollowReq = new AddFollowReq(1, 8, 17);
                HttpLoader.getInstance().sendRequest(new AddFollowHandler(addFollowReq, responseListener, 0));
                break;
            case R.id.btn_cancel_follow://取消关注
                CancelFollowReq cancelFollowReq = new CancelFollowReq(19, 1, 1);
                HttpLoader.getInstance().sendRequest(new CancelFollowHandler(cancelFollowReq, responseListener, 0));
                break;
            case R.id.btn_test_report://投诉
                ReportReq reportReq = new ReportReq(1, 1, 17);
                HttpLoader.getInstance().sendRequest(new ReportHandler(reportReq, responseListener, 0));
                break;
            case R.id.btn_after_share://分享之后调用
                TransferReq transferReq = new TransferReq(12, 1, 17);
                HttpLoader.getInstance().sendRequest(new TransferHandler(transferReq, responseListener, 0));
                break;
            case R.id.btn_test_collect://我-收藏
//                CollectReq collectReq = new CollectReq(1, 17);
//                HttpLoader.getInstance().sendRequest(new CollectHandler(collectReq, responseListener, 0));
                break;
            case R.id.btn_my_like://我-点赞
//                MyLikeReq myLikeReq = new MyLikeReq(1, 2, 17);
//                HttpLoader.getInstance().sendRequest(new MyLikeHandler(myLikeReq, responseListener, 0));
                break;
            case R.id.btn_test_stop_listen://我-结束听歌
                ListenReq listenReq = new ListenReq(1, 17);
                HttpLoader.getInstance().sendRequest(new ListenHandler(listenReq, responseListener, 0));
                break;
            case R.id.btn_test_comment://我-评论
                CommentReq commentReq = new CommentReq(2, 17, 1, "测试内容");
                HttpLoader.getInstance().sendRequest(new CommentHandler(commentReq, responseListener, 0));
                break;
            case R.id.btn_test_query://我-分类查询用户
                GetUserByCatReq getUserByCatReq = new GetUserByCatReq(GetUserByCatReq.ByTypeEnum.type, 1, 1, 1);
                HttpLoader.getInstance().sendRequest(new GetUserByCatHandler(getUserByCatReq, responseListener, 0));
                break;
            case R.id.btn_test_drama_category:
                GetListReq getListReq = new GetListReq(1, 1);
                HttpLoader.getInstance().sendRequest(new GetListHandler(getListReq, responseListener, 0));
                break;
            case R.id.btn_test_find_group_main:
                GetGroupMainReq getGroupMainReq = new GetGroupMainReq("杭州市");
                HttpLoader.getInstance().sendRequest(new GetGroupMainHandler(getGroupMainReq, responseListener, 0));
                break;
            case R.id.btn_test_group_detail:
//                GetGroupDetailReq getGroupDetailReq = new GetGroupDetailReq(1);
//                HttpLoader.getInstance().sendRequest(new GetGroupDetailHandler(getGroupDetailReq, responseListener, 0));
                break;
            case R.id.btn_test_query_group:
                QueryGroupListReq queryGroupListReq = new QueryGroupListReq(null, null, "recommend", QueryGroupListReq.TYPE_EXPERT, 10, 1);
                HttpLoader.getInstance().sendRequest(new QueryGroupListHandler(queryGroupListReq, responseListener, 0));
                break;
            case R.id.btn_test_part_group://发现-加入圈子
                PartInGroupReq partInGroupReq = new PartInGroupReq(1, 1);
                HttpLoader.getInstance().sendRequest(new PartInGroupHandler(partInGroupReq, responseListener, 0));
                break;
            case R.id.btn_test_book_list://发现-课本列表
                GetBookListReq getBookListReq = new GetBookListReq(10, 1);
                HttpLoader.getInstance().sendRequest(new GetBookListHandler(getBookListReq, responseListener, 0));
                break;
            case R.id.btn_test_update_sex://更新性别
                UpdateSexReq updateSexReq = new UpdateSexReq(17, "男");
                HttpLoader.getInstance().sendRequest(new UpdateSexHandler(updateSexReq, responseListener, 0));
                break;
            case R.id.btn_test_update_name://更新名字
                UpdateNameReq updateNameReq = new UpdateNameReq(17, "五月天");
                HttpLoader.getInstance().sendRequest(new UpdateNameHandler(updateNameReq, responseListener, 0));
                break;
            case R.id.btn_test_update_hobby://更新爱好
                UpdateHobbyReq updateHobbyReq = new UpdateHobbyReq(17, "打豆豆");
                HttpLoader.getInstance().sendRequest(new UpdateHobbyHandler(updateHobbyReq, responseListener, 0));
                break;
            case R.id.btn_test_update_teacher://更新师从
                UpdateTeacherReq updateTeacherReq = new UpdateTeacherReq(17, "五月天");
                HttpLoader.getInstance().sendRequest(new UpdateTeacherHandler(updateTeacherReq, responseListener, 0));
                break;
            case R.id.btn_test_update_zone://更新地区
                UpdateZoneReq updateZoneReq = new UpdateZoneReq(17, "杭州");
                HttpLoader.getInstance().sendRequest(new UpdateZoneHandler(updateZoneReq, responseListener, 0));
                break;
            case R.id.btn_test_get_community_dynamic://获取 社区-曲目动态
                GetCommunitySingDynamicListReq getCommunitySingDynamicListReq = new GetCommunitySingDynamicListReq(1, 10, 1, 1);
                HttpLoader.getInstance().sendRequest(new GetCommunitySingDynamicListHandler(getCommunitySingDynamicListReq, responseListener, 0));
                break;
            case R.id.btn_test_location_hobby://社区-同城同好
                GetLocationHobbyReq getLocationHobbyReq = new GetLocationHobbyReq(1, "test", 1, 1);
                HttpLoader.getInstance().sendRequest(new GetLocationHobbyHandler(getLocationHobbyReq, responseListener, 0));
                break;
            case R.id.btn_test_publish://发现-发布演出、活动、订票、参加活动
//                SavePublishReq savePublishReq = new SavePublishReq(1, "test内容", 17);
//                HttpLoader.getInstance().sendRequest(new SavePublishHandler(savePublishReq, responseListener, 0));
                break;
            case R.id.btn_test_ordered_sing://唱戏-已点戏
                MyOrderedSingReq myOrderedSingReq = new MyOrderedSingReq(1, 1, 10);
                HttpLoader.getInstance().sendRequest(new MyOrderedSingHandler(myOrderedSingReq, responseListener, 0));
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerEventBus();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unRegisterEventBus();
    }
}
